package com.example.weathersuggestion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class CurrentConditions {
	final static int CLEAR = 0;
	final static int RAIN = 1;
	final static int SNOW = 2;
	final static int TSTORMS = 3;
	final static int HAZY = 4;
	final static int CLOUDY = 5;
	final static int PARTLY_CLOUDY = 6;
	final static int MOSTLY_CLOUDY = 7;
	final static int NIGHT_PARTLY_CLOUDY= 8;
	final static int OTHER = -1;
	
	
	
	private double currentTemp, feelsLike;
	private String condition, obsTime, locationName; 
	private int conditionValue = 0;
	private double precipChance = 0;
	private boolean isvalid;

	public CurrentConditions(InputStream in){
		
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuffer buf = new StringBuffer();
		String str;
		 try {
			while ((str = br.readLine()) != null) {
			        buf.append(str);
			        buf.append("\n");
			}
			JSONObject jo = new JSONObject(buf.toString());		
			parseJSON(jo);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void parseJSON(JSONObject jo) {
	
			JSONObject co;
			try {
				co = jo.getJSONObject("current_observation");
				this.currentTemp = co.getDouble("temp_f");
				this.feelsLike =  co.getDouble("feelslike_f");
				this.condition = co.getString("icon");
				this.obsTime = co.getString("observation_time");
				this.precipChance = co.getDouble("precip_today_in");
				JSONObject ol = co.getJSONObject("display_location");
				this.locationName = ol.getString("full");
				isvalid = true;
				
			} catch (JSONException e) {
				isvalid = false;
			}

	
	}

	public boolean isValid(){
		return isvalid;
	}
	public String getImgDescrip(){
		return condition;
			
		
	}
	
	public int getCondition(){
	
		if(condition.equals("clear"))
			conditionValue = CLEAR;
		else if(condition.equals("rain"))
			conditionValue = RAIN;
		else if(condition.equals("tstorms"))
			conditionValue = TSTORMS;
		else if(condition.equals("hazy"))
			conditionValue = HAZY;
		else if(condition.equals("partlycloudy"))
			conditionValue = PARTLY_CLOUDY;
		else if (condition.equals("cloudy"))
			conditionValue = CLOUDY;
		else if (condition.equals("snow"))
			conditionValue = SNOW;
		else if (condition.equals("mostlycloudy"))
			conditionValue = MOSTLY_CLOUDY;
		else if(condition.equals("nt_partlycloudy"));
		else 
			conditionValue = OTHER;

		
		return conditionValue;
	}
	
	public double getTemp() {
		
		return currentTemp;
	}
public double getFeelsLikeTemp() {
		
		return feelsLike;
	}

public String getObservationTime() {
	
	return obsTime;
}

public Double getprecipChance(){
	return precipChance;
	
}

public String getObvsLocation() {
	// TODO Auto-generated method stub
	return locationName;
}
}
